/*-------------------------------------------------
	File		: AlertDialog.java
	Author		: Andik Taufiq
	DateTime	: 2008/08/22 11:43
---------------------------------------------------*/

import javax.microedition.lcdui.*;
import java.io.IOException;

public class AlertDialog {

	public String title;

	public String message;

	public int type;

	public boolean showed = false;

	public Image transparan;

	public String leftMenu = "";
	public String rightMenu = "";

	public int width;
	public int height;
	public int yArea;

	public static final int ALERTDIALOG_QUESTION		= 1;
	public static final int ALERTDIALOG_INFORMATION		= 2;
	public static final int ALERTDIALOG_WARNING			= 4;
	public static final int ALERTDIALOG_ERROR			= 3;
	public static final int ALERTDIALOG_PROGRESS		= 0;

	private Image[] icons;

	private Font plainFont;
    private Font boldFont;
	private Font underlinedFont;
	private Font italicFont;
	private Font mediumPlainFont;
    private Font mediumBoldFont;
	private Font mediumUnderlinedFont;
	private Font mediumItalicFont;

	// atribut-atribut yg terkait dengan penggambaran string deskripsi ke layar dalam bentuk wraptext
	private int textTop = 220;				// batas atas wraptext
	private int textWidth = 170;			// lebar maksimum untuk penulisan wraptext
	private int xIcons = 17;				// posisi x gambar icon
	private int xText = 55;					// posisi x untuk wraptext

	public AlertDialog(String title, String message, int type) {
		this.title		= title + " ";
		this.message	= message + " ";
		this.type		= type;
		plainFont 	= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        boldFont	= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
		underlinedFont 	= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED, Font.SIZE_SMALL);
		italicFont 	= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_ITALIC, Font.SIZE_SMALL);
		mediumPlainFont 	= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
        mediumBoldFont		= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
		mediumUnderlinedFont		= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_UNDERLINED, Font.SIZE_MEDIUM);
		mediumItalicFont		= Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_ITALIC, Font.SIZE_MEDIUM);
		icons = new Image[5];
		try {
            icons[0]		= Image.createImage("/clock-icon.png");
			icons[1]		= Image.createImage("/help-icon.png");
			icons[2]		= Image.createImage("/info-icon.png");
			icons[3]		= Image.createImage("/remove-icon.png");
			icons[4]		= Image.createImage("/warning-icon.png");
			transparan		= Image.createImage("/transparan.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
		switch (type) {
			case AlertDialog.ALERTDIALOG_PROGRESS:
				leftMenu = "";
				rightMenu = "Cancel";
				break;
			case AlertDialog.ALERTDIALOG_QUESTION:
				leftMenu = "Yes";
				rightMenu = "No";
				break;
			case AlertDialog.ALERTDIALOG_INFORMATION:
				leftMenu = "Ok";
				rightMenu = "";
				break;
			case AlertDialog.ALERTDIALOG_ERROR:
				leftMenu = "Ok";
				rightMenu = "";
				break;
			case AlertDialog.ALERTDIALOG_WARNING:
				leftMenu = "Ok";
				rightMenu = "";
				break;
		}
		height = icons[0].getHeight() + 10;
	}

	public void run() {
		if (showed == true)	{
			if (type == AlertDialog.ALERTDIALOG_PROGRESS) {
				try {
					Thread.sleep(3000);
				} catch (InterruptedException ex) {
					ex.printStackTrace();
				}
				showed = false;
			}
		}
	}

	public void paint(Graphics g) {
		if (showed == true)	{
			for (int i = 0; i < 24; i++) {
				for (int j = 0; j < 32; j++) {
					g.drawImage(transparan, i*transparan.getWidth(), j*transparan.getHeight(), Graphics.TOP | Graphics.LEFT);
				}
			}
			calculateArea(g, splitString(message), plainFont, 0x444444, 1, xText, textTop, textWidth);
			if (height < icons[0].getHeight() + 10) {
				height = icons[0].getHeight() + 10;
			}
			yArea = 320 - height - 30;
			g.setColor(0xd9e7f6);
			g.fillRect(6, yArea + 1, 228, height - 2);
			g.setColor(0xb8d0e9);
			g.fillRect(7, yArea + 2, 226, height - 4);
			textTop = yArea + 5;
			drawWrapText(g, splitString(message), plainFont, 0x444444, 1, xText, textTop, textWidth);
			g.drawImage(icons[type], xIcons, textTop, Graphics.LEFT | Graphics.TOP);
		}
	}

	public void setType(int type) {
		this.type = type;
		switch (type) {
			case AlertDialog.ALERTDIALOG_PROGRESS:
				leftMenu = "";
				rightMenu = "Cancel";
				break;
			case AlertDialog.ALERTDIALOG_QUESTION:
				leftMenu = "Yes";
				rightMenu = "No";
				break;
			case AlertDialog.ALERTDIALOG_INFORMATION:
				leftMenu = "Ok";
				rightMenu = "";
				break;
			case AlertDialog.ALERTDIALOG_ERROR:
				leftMenu = "Ok";
				rightMenu = "";
				break;
			case AlertDialog.ALERTDIALOG_WARNING:
				leftMenu = "Ok";
				rightMenu = "";
				break;
		}
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setMessage(String msg) {
		this.message = msg;
	}

	/*
	 * Split one lateral String parameter into array of words, blank characters are ignored.
	 *	@param:
	 *		String		str
	 *	@return:
	 *		String[]	result
	 *
	 *	@Andik, 2010-08-26
	 */
	private String[] splitString(String str) {
		int strLength = str.length();
		int nbWords = 0;
		boolean startCounting = false;
		for (int i = 0; i < strLength; i++) {
			if (str.charAt(i) == ' ') {
				if (startCounting == true) {
					nbWords++;
					startCounting = false;
				}
			} else {
				startCounting = true;
			}
			if (i == strLength - 1) {
				if (str.charAt(strLength-1) != ' ') {
					nbWords++;
					startCounting = false;
				}
			}
		}
		// for debugging purpose only ------
		//System.out.println("Jumlah Kata = " + nbWords);
		// ------------
		String[] result = new String[nbWords];
		for (int i = 0; i < nbWords; i++) {
			result[i] = "";
		}
		int resultIndex = 0;
		startCounting = false;
		for (int i = 0; i < strLength; i++) {
			if (str.charAt(i) == ' ') {
				if (startCounting == true) {
					startCounting = false;
					resultIndex++;
				}
			} else {
				startCounting = true;
				result[resultIndex] += str.charAt(i);
			}
		}
		// for debugging purpose only ------
		//for (int i = 0; i < nbWords; i++) {
		//	System.out.println(result[i]);
		//}
		// ------------
		return result;
	}
	
	/*
	 * Draw wrap text into graphics context from array of words with certain properties 
		and area parameters. Assumption: the value of area's width is reasonable enough 
		compared to the width of each words.
	 *	@param:
	 *		Graphics g
	 *		String[] words
	 *		Font f
	 *		int color
	 *		int space
	 *		int xArea
	 *		int yArea
	 *		int widthArea
	 *
	 *	@Andik, 2010-08-26
	 */
	public void drawWrapText(Graphics g, String[] words, Font f, int color, int space, 
		int xArea, int yArea, int widthArea) {
		int fontHeight = f.getHeight();
		int interval = fontHeight * space;
		int currentPos = xArea;
		int currentWidth = f.stringWidth(words[0]);
		int tempWidth = 0;
		g.setFont(f);
		g.setColor(color);
		for (int i = 0; i < words.length; i++) {
			if (tempWidth + f.stringWidth(words[i]) >= widthArea) {
				yArea += interval;
				currentPos = xArea;
			}
			g.drawString(words[i], currentPos, yArea, Graphics.LEFT | Graphics.TOP);
			currentWidth = f.stringWidth(words[i]) + 3;
			currentPos += currentWidth;
			tempWidth = currentPos - xArea;
			// for debugging purpose only ------
			//System.out.println(currentWidth);
			//System.out.println(words[i]);
			// ------------
		}
	}

	/*
	 * Calculate wrap text area.
	 *	@param:
	 *		Graphics g
	 *		String[] words
	 *		Font f
	 *		int color
	 *		int space
	 *		int xArea
	 *		int yArea
	 *		int widthArea
	 *
	 *	@Andik, 2010-08-27
	 */
	public void calculateArea(Graphics g, String[] words, Font f, int color, int space, 
		int xArea, int yArea, int widthArea) {
		int firstLine = yArea;
		int fontHeight = f.getHeight();
		int interval = fontHeight * space;
		int currentPos = xArea;
		int currentWidth = f.stringWidth(words[0]);
		int tempWidth = 0;
		g.setFont(f);
		g.setColor(color);
		for (int i = 0; i < words.length; i++) {
			if (tempWidth + f.stringWidth(words[i]) >= widthArea) {
				yArea += interval;
				currentPos = xArea;
			}
			currentWidth = f.stringWidth(words[i]) + 3;
			currentPos += currentWidth;
			tempWidth = currentPos - xArea;
		}
		this.height = yArea - firstLine + fontHeight + 10;
		// for debugging purpose only ------
		//System.out.println("Tingginya = " + this.height);
		// ------------
	}

	public void keyPressed(int keyCode) {
		if (showed == true) {
			switch (keyCode) {
				case -6:
					if (type != AlertDialog.ALERTDIALOG_PROGRESS) {
						showed = false;
					}
					break;
				case -7:
					if (type == AlertDialog.ALERTDIALOG_PROGRESS) {
						//showed = false;
					} else if (type == AlertDialog.ALERTDIALOG_QUESTION) {
						showed = false;
					}
					break;
				case -5:
					if (type != AlertDialog.ALERTDIALOG_PROGRESS) {
						showed = false;
					}
					break;
			}
		}
	}
}